﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Globalization;
using System.Windows.Forms;

namespace Barbar.UrlGuard.BL.Concrete {
  internal class AutoStartupService : IAutoStartupService {
    public const string RegistryPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
    public const string RegistryKey = "BarbarUrlGuard";

    public void SetAutoRun(bool autoRun) {
      if (autoRun)
        Registry.CurrentUser.OpenSubKey(RegistryPath, true).SetValue(RegistryKey, String.Format(CultureInfo.InvariantCulture, "\"{0}\"", Application.ExecutablePath));
      else
        Registry.CurrentUser.OpenSubKey(RegistryPath, true).DeleteValue(RegistryKey, false);
    }

    public bool IsAutoRunSet() {
      string value = (string)Registry.CurrentUser.OpenSubKey(RegistryPath, false).GetValue(RegistryKey, null);
      return !string.IsNullOrEmpty(value);
    }
  }
}
